/**
 * @file    contactlist.cpp
 * @author  Siddharth Sathyam <ssathy2@gmail.com>
 * @author  Alan Ruvalcaba <aruval3@gmail.com>
 * @author  Pat LaMontagna <plamontagna@gmail.com>
 * @version 1.0
 * @section LICENSE
 *
 * This program is property of Siddharth Sathyam, Alan Ruvalcaba, Pat LaMontagna.
 *
 * @section DESCRIPTION
 *
 * The contactlist class holds all of the friends of the user and displays the friends of the user.
 * This is the primary window of the application.
 */

#include "contactlist.h"
#include "ui_contactlist.h"
#include "chatwindow.h"
#include "removebuddy.h"
#include "addbuddy.h"
#include "settings.h"
ContactList::ContactList(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ContactList)
{
    ui->setupUi(this);
    this->setWindowTitle("Contact List");

    // Hardcode the ExitButton attributes
    ui->ExitButton->setIcon(QIcon(":icons/exitFriends.png"));
    ui->ExitButton->setIconSize(QSize (41,31));
    ui->ExitButton->setFlat(true);

    // Hardcode the AddBuddy attributes
    ui->AddBuddy->setIcon(QIcon(":icons/add.png"));
    ui->AddBuddy->setIconSize(QSize (41,31));
    ui->AddBuddy->setFlat(true);

    // Hardcode the RemoveBuddy attributes
    ui->RemoveBuddy->setIcon(QIcon(":icons/remove.png"));
    ui->RemoveBuddy->setIconSize(QSize (41,31));
    ui->RemoveBuddy->setFlat(true);

    // Hardcode the Settings attributes
    ui->Settings->setIcon(QIcon(":icons/settings.png"));
    ui->Settings->setIconSize(QSize (41,31));
    ui->Settings->setFlat(true);

    ui->requestsButton->setIconSize(QSize(41,31));
    ui->requestsButton->setFlat(true);
}

ContactList::ContactList(QWidget *parent, Client *client):
    QMainWindow(parent),
    ui(new Ui::ContactList)
{
    ui->setupUi(this);

    this->mainSoc = client;
    userName = mainSoc->getUserName();

    this->setWindowTitle(userName + "'s Contact List");

    // Hardcode the ExitButton attributes
    ui->ExitButton->setIcon(QIcon(":icons/exitFriends.png"));
    ui->ExitButton->setIconSize(QSize (50,40));
    ui->ExitButton->setFlat(true);

    // Hardcode the AddBuddy attributes
    ui->AddBuddy->setIcon(QIcon(":icons/add.png"));
    ui->AddBuddy->setIconSize(QSize (50,40));
    ui->AddBuddy->setFlat(true);

    // Hardcode the RemoveBuddy attributes
    ui->RemoveBuddy->setIcon(QIcon(":icons/remove.png"));
    ui->RemoveBuddy->setIconSize(QSize (50,40));
    ui->RemoveBuddy->setFlat(true);

    // Hardcode the Settings attributes
    ui->Settings->setIcon(QIcon(":icons/settings.png"));
    ui->Settings->setIconSize(QSize (50,40));
    ui->Settings->setFlat(true);

    // Hardcode the friendRequest attribute
    ui->requestsButton->setIcon(QIcon(":icons/friendRequest.png"));
    ui->requestsButton->setIconSize(QSize(50,40));
    ui->requestsButton->setFlat(true);

    chatList = new QVector<chatWindow*>();

    requestsWindow = new friendRequestsWindow(0, userName);

    connect(mainSoc, SIGNAL(receiveMessage(QString,QString,QString,QString)), this, SLOT(receiveMessage(QString,QString,QString,QString)));
    connect(mainSoc, SIGNAL(signalUsers(QStringList)), this, SLOT(populateList(QStringList)));
    connect(mainSoc, SIGNAL(friendRequest(QString)), requestsWindow, SLOT(handleFriendshipConfirmation(QString)));
    connect(requestsWindow, SIGNAL(confirmFriendRequest(QString,bool)), mainSoc, SLOT(handleFriendRequestConfirmation(QString,bool)));
}

ContactList::~ContactList()
{
    delete ui;
    requestsWindow->deleteLater();
    mainSoc->deleteLater();
}

void ContactList::on_listWidget_itemClicked(QListWidgetItem* item)
{
    for(int i = 0; i < this->chatList->size();i++)
    {
	if(this->chatList->at(i)->getToUser() == item->text())
	{
	    chatList->at(i)->show();
	    return;
	}
    }
    chatWindow *newChatWin = new chatWindow();
    connect(newChatWin,SIGNAL(messageSent(QString,QString,QString,QString)),this,SLOT(sendMessage(QString,QString,QString,QString)));
    newChatWin->setToUser(item->text());
    newChatWin->setfromUser(this->mainSoc->getUserName());
    newChatWin->setWindowTitle("Conversation with " + item->text());
    newChatWin->show();
    chatList->append(newChatWin);
}

void ContactList::populateList(QStringList listOfUser)
{
    this->ui->listWidget->clear();

    foreach (QString user, listOfUser)
    {
	if(user != this->userName)
	{
	    this->ui->listWidget->addItem(user);
	}
    }
}

void ContactList::sendMessage(QString from,QString to,QString message,QString time)
{
    this->mainSoc->sendMessage(from,to,message,time);
}

void ContactList::receiveMessage(QString fromUser,QString toUser,QString message,QString timeSent)
{


    for(int i =0; i < chatList->size();i++)
    {
	if(chatList->at(i)->getToUser() == fromUser)
	{
	    chatList->at(i)->receiveMessage(message,fromUser,timeSent);
	    return;
	}
    }
    chatWindow *newWin = new chatWindow();
    connect(newWin,SIGNAL(messageSent(QString,QString,QString,QString)),this,SLOT(sendMessage(QString,QString,QString,QString)));
    newWin->setfromUser(toUser);
    newWin->setToUser(fromUser);
    chatList->append(newWin);
    newWin->setWindowTitle("Conversation with " + fromUser);
    newWin->receiveMessage(message,fromUser,timeSent);
    newWin->show();
}

void ContactList::on_ExitButton_clicked()
{
    this->close();
}

void ContactList::on_Settings_clicked()
{
    settings *userSettingsWindow = new settings(0);

    connect(this,SIGNAL(passName(QString)),userSettingsWindow,SLOT(setUser(QString)));
    connect(this,SIGNAL(passHost(QString)),userSettingsWindow,SLOT(setHost(QString)));
    connect(this,SIGNAL(passNumFriends(QString)),userSettingsWindow,SLOT(setNumFriends(QString)));

    userSettingsWindow->setWindowTitle("User Settings");

    emit passName(this->mainSoc->getUserName());
    emit passHost(this->mainSoc->getAddress());
    emit passNumFriends(QString::number(ui->listWidget->count()));

    userSettingsWindow->show();
}

void ContactList::on_RemoveBuddy_clicked()
{
    removeBuddy *removeBuddyWindow = new removeBuddy(0, userName);

    connect(removeBuddyWindow, SIGNAL(sendRemoveFriendRequest(QString)), mainSoc, SLOT(handleFriendRemoveRequest(QString)));
    connect(mainSoc, SIGNAL(friendRemoveExistsConfirmation(QString,bool)), removeBuddyWindow, SLOT(handleIncomingIsValidRemoveFriendRequest(QString,bool)));
    connect(removeBuddyWindow, SIGNAL(finished(int)),
	    this, SLOT(handleRemoveBuddyWindowClosed()));

    removeBuddyWindow->setWindowTitle("Remove Buddy");
    removeBuddyWindow->show();
}

void ContactList::on_AddBuddy_clicked()
{
    addBuddy *addBuddyWindow = new addBuddy(0, userName);

    connect(addBuddyWindow, SIGNAL(addFriend(QString)), mainSoc, SLOT(handleOutgoingFriendRequest(QString)));
    connect(mainSoc, SIGNAL(friendExistsConfirmation(QString,bool)), addBuddyWindow, SLOT(handleIsValidFriendRequestConfirmation(QString,bool)));
    connect(addBuddyWindow, SIGNAL(finished(int)),
	    this, SLOT(handleAddBuddyWindowClosed()));

    addBuddyWindow->setWindowTitle("Add Buddy");

    addBuddyWindow->show();
}

// Have to disconnect the slots because of a wierd bug that was occurring
void ContactList::handleAddBuddyWindowClosed()
{
    addBuddy *addBuddyWindow = (addBuddy*)sender();

    disconnect(addBuddyWindow, SIGNAL(addFriend(QString)),
	       mainSoc, SLOT(handleOutgoingFriendRequest(QString)));
    disconnect(mainSoc, SIGNAL(friendExistsConfirmation(QString,bool)),
	       addBuddyWindow, SLOT(handleIsValidFriendRequestConfirmation(QString,bool)));

    addBuddyWindow->deleteLater();
}

// Have to disconnect the slots because of a wierd bug that was occurring
void ContactList::handleRemoveBuddyWindowClosed()
{
    removeBuddy *removeBuddyWindow = (removeBuddy*)sender();
    disconnect(removeBuddyWindow, SIGNAL(sendRemoveFriendRequest(QString)),
	       mainSoc, SLOT(handleFriendRemoveRequest(QString)));

    disconnect(mainSoc, SIGNAL(friendRemoveExistsConfirmation(QString,bool)),
	       removeBuddyWindow, SLOT(handleIncomingIsValidRemoveFriendRequest(QString,bool)));

    removeBuddyWindow->deleteLater();
}

void ContactList::on_requestsButton_clicked()
{
    requestsWindow->show();
}

void ContactList::closeEvent(QCloseEvent *event)
{
    for(int i = 0; i < chatList->size(); i++)
    {
	delete chatList->at(i);
    }

    delete chatList;
    event->accept();
}
